package com.erpanderp.wam.cm.model.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.cm.model.entity.EmpPersonalResume;
import com.erpanderp.wam.tm.model.entity.Band;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.HibernateUtil;

public class UploadResumeDAO {

	public EmpPersonalResume createEmployeePersonalResume(EmpPersonalResume employeeResume){
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			session.saveOrUpdate(employeeResume);
			session.getTransaction().commit();
			return employeeResume;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
		
	}
	public boolean getEmployeeResume(Long employeeId){
		boolean resumeUploaded = false;
		EmpPersonalResume employeeResume = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
		    String queryString = "from EmpPersonalResume er where er.employee.employeeId =:employeeId";
		    Query query = session.createQuery(queryString).setLong("employeeId",employeeId);
		    employeeResume = (EmpPersonalResume) query.uniqueResult();
		    if(employeeResume != null){
		    	resumeUploaded = true;
		    }
		    
			session.getTransaction().commit();
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return resumeUploaded;
		
	}
	public EmpPersonalResume getEmployeeResumeByEmployeeId(Long employeeId){		
		EmpPersonalResume employeeResume = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
		    String queryString = "from EmpPersonalResume er where er.employee.employeeId =:employeeId";
		    Query query = session.createQuery(queryString).setLong("employeeId",employeeId);
		    List<EmpPersonalResume>  employeeResumeList = query.list();		    
			session.getTransaction().commit();
			if(employeeResumeList != null && employeeResumeList.size()>0){
				employeeResume = employeeResumeList.get(0);
				return employeeResume;
			}else{
				return null;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public List<EmpPersonalResume> getAllResumes(String moduleName, String location, String employeeCode, String employeeName){
		List<EmpPersonalResume> employees = new ArrayList<EmpPersonalResume>();
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			String queryString = "from EmpPersonalResume e where e.status = :status";
			
			if(moduleName != null && !moduleName.equals("")){
				queryString += " and e.employee.moduleName = :moduleName";
			}
			
			if(location != null && !location.equals("")){
				queryString += " and e.employee.location = :location";
			}
			
			if(employeeCode != null && !employeeCode.equals("")){
				queryString += " and e.employee.employeeCode = :employeeCode";
			}
			
			if(employeeName != null && !employeeName.equals("")){
				queryString += " and (e.employee.firstName = :employeeName or e.employee.lastName = :employeeName)";
			}
			
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status", CommonLookup.STATUS_ACTIVE);
			
			if(moduleName != null && !moduleName.equals("")){
				query.setString("moduleName", moduleName);
			}
			
			if(location != null && !location.equals("")){
				query.setString("location", location);
			}
			
			if(employeeCode != null && !employeeCode.equals("")){
				query.setString("employeeCode", employeeCode);
			}
			
			if(employeeName != null && !employeeName.equals("")){
				query.setString("employeeName", employeeName);
			}			
			
			employees =(List<EmpPersonalResume>) query.list();
			session.getTransaction().commit();
			return employees;	
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}		
	}
	
}
